from sanic import Blueprint
from controllers.backtest import BacktestController
from decorators import response_json
from utils.parser import parse_args

bp = Blueprint("backtest", url_prefix="/backtest")


@bp.route("/income_times_run", methods=["POST"])
@response_json
async def income_times_run(request):
    """ income times backtest """
    args_rules = [
        dict(arg="strategy_name", required=True, type="str"),
        dict(arg="from_date", dest="from_date_int", required=True, type="int"),
        dict(arg="low_price", required=True, type="int"),
        dict(arg="high_price", required=True, type="int"),
        dict(arg="days", required=True, type="int"),
        dict(arg="percent", required=True, type="int"),
    ]
    args = parse_args(request, "json", args_rules)
    return BacktestController.income_times(args)


@bp.route("/<id:int>/kill", methods=["GET"])
@response_json
async def kill(request, id):
    BacktestController(id).kill_run()
    return {}


@bp.route("", methods=["GET"])
@response_json
async def backtest(request):
    return BacktestController.all_backtest()


@bp.route("/<id:int>", methods=["DELETE", "GET"])
@response_json
async def backtest_id(request, id):
    if request.method == "DELETE":
        BacktestController(id).delete()
        return {}
    elif request.method == "GET":
        return BacktestController(id).backtest


@bp.route("/<id:int>/record", methods=["GET"])
@response_json
async def backtest_record(request, id):
    return BacktestController(id).record

